//
//  HotsDetailViewController.m
//  QuicklyPlay
//
//  Created by 郭晓宇 on 15/11/3.
//  Copyright © 2015年 OPteam. All rights reserved.
//

#import "HotsDetailViewController.h"
#import "UMSocial.h"
#import "NetWorkViewController.h"
#import <MediaPlayer/MediaPlayer.h>
#import "PlayingViewController.h"
#import "SortDetailViewController.h"
#import "CollectionListViewController.h"
#import "DataManager.h"
#import "MPPlayViewController.h"
#define UmengAppkey @"5424c4b6fd98c58f73022709"


@interface HotsDetailViewController ()

////进行判断状态的Bool值:
//@property(nonatomic,assign)BOOL isDianZan;

//创建用于(点赞)提示的label:
@property(nonatomic,strong)UILabel *label;
@property(nonatomic,assign)NSInteger intString;
@property(nonatomic,strong)NSString *zanshu;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *AutoSizeHeight;
@property (weak, nonatomic) IBOutlet UINavigationBar *hotsNavigationBar;
@property (weak, nonatomic) IBOutlet UIWebView *htmlWebView;
@end

@implementation HotsDetailViewController

-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
//        _isDianZan = YES;
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];

    
    
    
    NSString *newString = [NSString stringWithFormat:@"%@",self.reModel.digs];
    self.intString = [newString intValue];
    
    if (self.reModel.isDianzan == YES)
    {
    self.zanshu = [NSString stringWithFormat:@"%ld 赞",self.intString+1];
    self.zanshuLabel.text = self.zanshu;
    [self setUpView1];
    }
    else if(self.reModel.isDianzan == NO)
    {
    self.zanshuLabel.text = newString;
    [self setUpView2];
    }
    
    [[GestureRecognizerTool creatScreenEdgePanGestureRecognizerLeftWithViewController:self] addTarget:self action:@selector(bacoTo:)];
    
    
}



- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    

    if ([self.reModel.html isEqualToString:@""]) {
        self.htmlWebView.hidden = YES;
    } else {
        
        self.playImageView.hidden = YES;
        self.htmlWebView.hidden = NO;
        [self.htmlWebView loadHTMLString:self.reModel.html baseURL:nil];
        self.htmlWebView.scrollView.bounces = NO;
        self.htmlWebView.scrollView.scrollEnabled = NO;
    }

}




-(void)setUpView1{
    //给点赞label上面的点赞imageView添加手势:
    self.dianzanImageView.userInteractionEnabled = YES;
    UIImage *likeImage = [UIImage imageNamed:@"dianzanluse.png"];
    [self.dianzanImageView setImage:likeImage];

    UITapGestureRecognizer *dianzanTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dianzanAction:)];
    [self.dianzanImageView addGestureRecognizer:dianzanTap];
    [self setUpAllViews];
}


-(void)setUpView2{
    //给点赞label上面的点赞imageView添加手势:
    self.dianzanImageView.userInteractionEnabled = YES;
    UIImage *likeImage = [UIImage imageNamed:@"dianzan.png"];
    [self.dianzanImageView setImage:likeImage];
    
    UITapGestureRecognizer *dianzanTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dianzanAction:)];
    [self.dianzanImageView addGestureRecognizer:dianzanTap];
    [self setUpAllViews];
}
//加载视图控件:
-(void)setUpAllViews{
    //视频的简介label:
    self.titleLabel.text = self.reModel.title;
    
    //视频的发布时间:
    NSTimeInterval time = [self.reModel.publish_time doubleValue];
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:time];
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:@"发布:MM月dd日 HH:mm"];
    self.fabuLabel.text = [formatter stringFromDate:date];
    
    //图片:
    [self.playImageView sd_setImageWithURL:[NSURL URLWithString:self.reModel.img_url]];
    
    //播放次数的label:
     NSString *Videos = [NSString stringWithFormat:@"浏览:%@次",self.reModel.plays];
    self.playLabel.text = Videos;
    
    //zanLabel添加边框:
//    self.zanLabel.layer.borderColor = (__bridge CGColorRef _Nullable)([UIColor blueColor]);
    self.zanLabel.layer.borderColor = [UIColor colorWithRed:254/255.0 green:201/255.0 blue:32/255.0 alpha:1].CGColor;
    self.zanLabel.layer.borderWidth = 1;
    self.zanLabel.layer.cornerRadius = 10;
    
    //三个按钮的属性:
    //下载:
//    [self.loginButton.layer setMasksToBounds:YES];
//    [self.loginButton.layer setCornerRadius:10.0]; //设置矩形四个圆角半径
//    [self.loginButton.layer setBorderWidth:1.0]; //边框宽度
//    CGColorSpaceRef colorSpace1 = CGColorSpaceCreateDeviceRGB();
//    CGColorRef colorref1 = CGColorCreate(colorSpace1,(CGFloat[]){ 1, 0, 0, 1 });
//    [self.loginButton.layer setBorderColor:colorref1];//边框颜色
//    [self.loginButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];//title color
//    self.loginButton.backgroundColor = [UIColor whiteColor];
//    self.loginButton = [UIButton buttonWithType:UIButtonTypeSystem];
//    [self.loginButton setTitle:@"下载" forState:UIControlStateNormal];
//    [self.loginButton setTintColor:[UIColor whiteColor]];
    //收藏:
    [self.collectionButton.layer setMasksToBounds:YES];
    [self.collectionButton.layer setCornerRadius:10.0]; //设置矩形四个圆角半径
    [self.collectionButton.layer setBorderWidth:1.0]; //边框宽度
    
    //CGColorSpaceRef colorSpace2 = CGColorSpaceCreateDeviceRGB();
    //CGColorRef colorref2 = CGColorCreate(colorSpace2,(CGFloat[]){ 1, 0, 0, 1 });
    //[self.collectionButton.layer setBorderColor:colorref2];//边框颜色
    self.collectionButton.layer.borderColor = [UIColor colorWithRed:254/255.0 green:201/255.0 blue:32/255.0 alpha:1].CGColor;
    [self.collectionButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];//title color
    self.collectionButton.backgroundColor = [UIColor whiteColor];
    
    self.collectionButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [self.collectionButton setTitle:@"下载" forState:UIControlStateNormal];
    [self.collectionButton setTintColor:[UIColor whiteColor]];
    //分享:
    [self.fenxiangButton.layer setMasksToBounds:YES];
    [self.fenxiangButton.layer setCornerRadius:10.0]; //设置矩形四个圆角半径
    [self.fenxiangButton.layer setBorderWidth:1.0]; //边框宽度
    
    //CGColorSpaceRef colorSpace3 = CGColorSpaceCreateDeviceRGB();
    //CGColorRef colorref3 = CGColorCreate(colorSpace3,(CGFloat[]){ 1, 0, 0, 1 });
    //[self.fenxiangButton.layer setBorderColor:colorref3];//边框颜色
    self.fenxiangButton.layer.borderColor = [UIColor colorWithRed:254/255.0 green:201/255.0 blue:32/255.0 alpha:1].CGColor;
    [self.fenxiangButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];//title color
    self.fenxiangButton.backgroundColor = [UIColor whiteColor];
    
    
    self.fenxiangButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [self.fenxiangButton setTitle:@"下载" forState:UIControlStateNormal];
    [self.fenxiangButton setTintColor:[UIColor whiteColor]];


    //给播放label上面的imageView添加手势:
    self.playImageView.userInteractionEnabled = YES;
    UITapGestureRecognizer *playTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(playActon:)];
    [self.playImageView addGestureRecognizer:playTap];
    
   }


//点赞label上面imageView的手势:
-(void)dianzanAction:(UITapGestureRecognizer *)sender{
    NSLog(@"imageView的点击手势");
//    NSString *newString = [NSString stringWithFormat:@"%@",self.reModel.digs];
//    self.intString = [newString intValue];
    if (YES == self.reModel.isDianzan) {
        UIImage *likeImage = [[UIImage imageNamed:@"dianzan.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        [self.dianzanImageView setImage:likeImage];
        self.intString = self.intString - 1;
        self.zanshu = [NSString stringWithFormat:@"%ld 赞",self.intString];

        self.zanshuLabel.text = self.zanshu;
        
        //取消点赞成功:
        //[self setcapion1];
        
    }else if(NO == self.reModel.isDianzan){
        UIImage *likeImage = [[UIImage imageNamed:@"dianzanluse.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        [self.dianzanImageView setImage:likeImage];
        self.intString = self.intString + 1;
        self.zanshu = [NSString stringWithFormat:@"%ld 赞",self.intString];
        self.zanshuLabel.text = self.zanshu;
        //点赞成功:
        [self setcapion];
    }
    //更新选中的状态:
    self.reModel.isDianzan = !self.reModel.isDianzan;
    
}
//提示信息:
-(void)setcapion
{
    //创建一个label，用于提示的信息
    _label = [[UILabel alloc] init];
    _label.translatesAutoresizingMaskIntoConstraints = NO;
    
    _label.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];
    
    [self.view addSubview:_label];

    [_label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view.mas_centerX);
        make.centerY.equalTo(self.view.mas_centerY).offset(-50);
        make.height.mas_equalTo(30);
        make.width.mas_equalTo(70);
    }];
    
    
    _label.text = @"点赞成功";
    _label.layer.cornerRadius = 10;
    _label.layer.borderWidth = 1;
    _label.layer.borderColor = [UIColor colorWithRed:254/ 255.0 green:201/255.0 blue:32 / 255.0 alpha:1].CGColor;
    _label.layer.masksToBounds = YES;
    _label.textColor = [UIColor whiteColor];
    _label.font = [UIFont systemFontOfSize:15];
    _label.textAlignment = NSTextAlignmentCenter;
    
    //创建定时器，用于label在0.3秒之后自动消失:
    [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(doTimer) userInfo:nil repeats:NO];
    
}

/*
-(void)setcapion1
{
    //创建一个label，用于提示的信息
    _label = [[UILabel alloc] initWithFrame:CGRectMake(170, 300, 75, 30)];
    _label.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];
    _label.text = @"点赞取消";
    
    
    _label.layer.cornerRadius = 10;
    _label.layer.borderWidth = 1;
    _label.layer.borderColor = [UIColor blackColor].CGColor;
    _label.layer.masksToBounds = YES;
    _label.textColor = [UIColor whiteColor];
    _label.font = [UIFont systemFontOfSize:15];
    _label.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:_label];
    
    //创建定时器，用于label的消失
    [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(doTimer) userInfo:nil repeats:NO];
    
}
 
 */

//实现时候将label移除

-(void)doTimer
{
    //实现在1秒之后清除label:
    [_label removeFromSuperview];
}




//playImage的点击事件实现播放:
-(void)playActon:(UITapGestureRecognizer *)sender{
    NSLog(@"开始播放");
 
    if ([NetworkHelper judgeNetworkStatus]) {
        MPPlayViewController *playController = [[MPPlayViewController alloc] initWithContentURL:[NSURL URLWithString:self.reModel.m3u8_url]];
        
        [self presentMoviePlayerViewControllerAnimated:playController];
    }
    

    
    
//        PlayingViewController *playVC = [[PlayingViewController alloc] init];
//        playVC.getModel = self.reModel;
//        [self presentViewController:playVC animated:YES completion:nil];
    
    

}


//返回:
- (IBAction)bacoTo:(id)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
    //self.view.hidden = YES;
}

//网络的方法:
- (IBAction)netWorkItemAction:(id)sender {
    NSLog(@"网络跳转");
    NetWorkViewController *web = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"netWorkVC"];
    [self presentViewController:web animated:YES completion:nil];
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:self.reModel.web_url]];
    [web.webView loadRequest:request];
    
}


//分享的方法:
- (IBAction)fenxiangButtonAction:(id)sender {
    [UMSocialSnsService presentSnsIconSheetView:self appKey:@"5649a0eb67e58ebef3000074"shareText:self.reModel.share_url shareImage:nil shareToSnsNames:[NSArray arrayWithObjects:UMShareToSina,UMShareToTencent,UMShareToRenren,UMShareToDouban,nil] delegate:nil];
}

- (BOOL)shouldAutorotate {
    return NO;
}



//收藏
- (IBAction)collectionButton:(UIButton *)sender {
    //先判断是否为登陆状态 , 非登陆状态不能收藏 , 登陆状态可以收藏]
    
    
    //归档
//    [[collectionHelpers shareInstance]celloction:self.reModel];
//    NSString*string = self.reModel.title ;
//    NSData*data = [string dataUsingEncoding:NSUTF8StringEncoding] ;
    
    DataManager *manager = [DataManager sharedInstance];
   NSString *message = [manager saveDataWithDicti:self.reModel];

    
    //登陆状态 收藏
    UIAlertView*alertV = [[UIAlertView alloc]initWithTitle:@"提示" message:message delegate:self cancelButtonTitle:@"确定" otherButtonTitles:@"打开收藏列表",nil] ;
    [alertV show] ;
}

//监听alert 直接跳转到收藏列表
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (buttonIndex == 1) {
        //NSLog(@"hhaha");
        CollectionListViewController *collectionVC = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"CollectionVC"];
        [self presentViewController:collectionVC animated:YES completion:nil] ;
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
